package org.example.spring_blog.controller;


import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.example.spring_blog.model.BlogInfo;
import org.example.spring_blog.service.BlogInfoService;
import org.example.spring_blog.util.jwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    BlogInfoService blogInfoService;
    @RequestMapping("/getList")
    public List<BlogInfo> getList() {
        List<BlogInfo> blogInfos = blogInfoService.selectAll();
        return blogInfos;
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request) {
        if(blogId==null) {
            throw new UnsupportedOperationException("参数不能为null");
        }
        BlogInfo blogInfo = blogInfoService.selectById(blogId);
        Integer authorId=blogInfo.getId();
        String jwt=request.getHeader("user_token_header");
        Assert.notNull(jwt,"未登录");
        Integer userId = jwtUtils.getUserIdFromToken(jwt);
        if (authorId!=null && authorId==userId){
            blogInfo.setIsLoginUser(true);
        }
        return blogInfo;
    }

    @RequestMapping("/add")
    public Boolean addBlog (@Validated @RequestBody BlogInfo blogInfo, HttpServletRequest request) {
        log.info("addBlog,接收参数："+blogInfo);
        String jwt=request.getHeader("user_token_header");
        Assert.notNull(jwt,"未登录");
        Integer userId = jwtUtils.getUserIdFromToken(jwt);
        blogInfo.setUserId(userId);
        try {
           int ret = blogInfoService.addBlog(blogInfo);
           if(ret==1) {
               return true;
           }
        }catch (Exception e) {
            log.error("添加失败,e:",e);
        }
        return false;
    }

    @RequestMapping("/update")
    public Boolean updateBlog(@Validated @RequestBody BlogInfo blogInfo) {
        log.info("updateBlog,接收参数："+blogInfo);
        Integer ret=null;
        try {
            ret=blogInfoService.updateBlog(blogInfo);
            if(ret>=1) {
                return true;
            }
        }catch (Exception e) {
            log.error("更新失败,e:",e);
        }
        return false;
    }

    @RequestMapping("/delete")
    public Boolean deleteBlog(Integer blogId){
        log.info("deleteBlog,接收参数："+blogId);
        Integer ret=null;
        try {
            BlogInfo blogInfo=new BlogInfo();
            blogInfo.setId(blogId);
            blogInfo.setDeleteFlag(1);
            ret=blogInfoService.deleteBlog(blogInfo);
            if(ret>=1) {
                return true;
            }
        }catch (Exception e) {
            log.error("更新失败,e:",e);
        }
        return false;
    }
}
